package top.frsf.input;

import lombok.extern.slf4j.Slf4j;
import top.frsf.enums.GameStateEnum;
import top.frsf.factory.PlantFactory;
import top.frsf.object.card.CardObject;
import top.frsf.object.env.Grass;
import top.frsf.object.env.Sun;
import top.frsf.object.plant.PlantObject;
import top.frsf.panel.GamePanel;
import top.frsf.starter.impl.CardStarter;
import top.frsf.starter.impl.EnvStarter;
import top.frsf.starter.impl.HomeStarter;
import top.frsf.starter.impl.PlantStarter;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import static top.frsf.panel.GamePanel.starterContext;

/**
 * 鼠标事件
 *
 * @author FRSF
 * @since 2025/8/1 15:41
 */
@Slf4j
public class MouseAction extends MouseAdapter {
    /**
     * 鼠标实体对象
     */
    private final Rectangle click = new Rectangle(20, 20);
    /**
     * 鼠标移动坐标
     */
    public static final Point move = new Point(0, 0);
    /**
     * 移动的卡片对象
     */
    public static CardObject moveCardObj = null;


    @Override
    public void mouseMoved(MouseEvent e) {
        // 若存在选择中的卡片，则不停记录鼠标坐标，用于鼠标悬浮Card图像
        if (GamePanel.state == GameStateEnum.HOME) {
            HomeStarter homeStarter = starterContext.getStarter(HomeStarter.class);
            Rectangle adventureButton = homeStarter.getAdventureButton();
            homeStarter.setAdventureLight(adventureButton.contains(e.getPoint()));
        }

        if (moveCardObj != null) {
            move.setLocation(e.getX(), e.getY());
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        click.setLocation(e.getX(), e.getY());
        log.info("mouse click: {}", click.getLocation());

        EnvStarter envStarter = starterContext.getStarter(EnvStarter.class);
        CardStarter cardStarter = starterContext.getStarter(CardStarter.class);
        PlantStarter plantStarter = starterContext.getStarter(PlantStarter.class);
        HomeStarter homeStarter = starterContext.getStarter(HomeStarter.class);

        // HOME开启游戏,（后续程序不执行）
        if (GamePanel.state == GameStateEnum.HOME || homeStarter.getAdventureButton().contains(e.getPoint())) {
            GamePanel.state = GameStateEnum.START;
            return;
        }

        // 检测是否点击了阳光
        for (Sun sun : envStarter.getSuns()) {
            if (click.intersects(sun.getRectangle())) {
                envStarter.getRmvSuns().add(sun);
                envStarter.setSunsCount(envStarter.getSunsCount() + 50);
                return;
            }
        }

        // 检测是否进行植物卡片安放
        if (moveCardObj != null) {
            // 遍历草地，寻找能安放位置
            for (Grass grid : envStarter.getGrids()) {
                if (grid.isAllowPlant() && click.intersects(grid.getRectangle())) {
                    // 草地可种植 && 鼠标点击位置在草地内
                    grid.setAllowPlant(false);
                    ArrayList<PlantObject> plants = plantStarter.getPlants();
                    plants.add(PlantFactory.createPlant(moveCardObj.getPlantTypeEnum(), grid.getX(), grid.getY()));
                    moveCardObj = null;
                    return;
                }
            }
        }

        // 检测是否点击了植物卡片
        for (CardObject card : cardStarter.getCards()) {
            if (click.intersects(card.getRectangle())) {
                Integer sunsCount = envStarter.getSunsCount();
                // 阳光充足 && 卡片未冷却
                if (sunsCount >= card.getPrice() && !card.isInCd()) {
                    // 设置点击坐标
                    move.setLocation(e.getX(), e.getY());
                    // 设置选中卡片
                    moveCardObj = card;
                    // 设置卡片冷却
                    card.setInCd(true);
                    card.setLastCdTime(System.currentTimeMillis());
                    // 减去阳光
                    envStarter.setSunsCount(sunsCount - card.getPrice());
                }
            }
        }
    }
}
